<?php
namespace shenka\controllers;

use Yii;
use yii\web\Controller;
use yii\db\Connection;
use shenka\controllers\Edition;
use shenka\controllers\Select;
include 'Edition.php';
include 'Select.php';
/*
* @author admin 
* @class Edition
* @desc 神卡系列产品数据查询
*/

class EditionController extends Controller {
	/*
	* @author admin 
	* @function index
	* @desc 神卡系列产品数据查询
	*/
    public $layout = 'second';
	public function actionIndex(){
		$view = Yii::$app->view;
		$view->params['layout_data'] = '神卡系列产品数据查询';

		return $this->render('index');
	}
	 // ajax 根据产品类型 选择查询方式
    public function actionType(){
        if(Yii::$app->request->isAjax){
            $type=isset($_POST['t'])? $_POST['t']: '';
            if(empty($type)){
                return '请选择产品';
            }
            $dataArray=json_decode(file_get_contents("../controllers/type.json"),true);
            $data=json_encode($dataArray[$type]);
            return $data;
        }
    }
    public function actionSubmit(){
        if(Yii::$app->request->isAjax){
            $edition=isset($_POST['edition'])? $_POST['edition']: '';
            $type=isset($_POST['type'])? $_POST['type']: '';
            $data=isset($_POST['data'])? $_POST['data']: '';
            switch($edition){
                case '1':
                    $modelData=new Star($this->DbConfig('star'));
                    break;
                case '2':
                    $modelData=new Press($this->DbConfig('press'));
                    break;
                case '3':
                    $modelData=new Kr($this->DbConfig('kr'));
                    break;
                case '4':
                    $modelData=new Dinosaur($this->DbConfig('dinosaur'));
                    break;
                case '5':
                    $modelData=new Edu($this->DbConfig('edu'));
                    break;
                case '6':
                    $modelData=new Art($this->DbConfig('art'));
                    break;
                default:
                    echo '参数传递错误！';
            }
            //装饰
            switch($type){
                case 'sn':
                    $modelData=new Sn($modelData);
                    $modelData=new Phone($modelData);
                    $list=$modelData->Querys(['sn'=>$data]);
                    break;
                case 'phonecode':
                    $modelData=new PhoneCode($modelData);
                    $list=$modelData->Querys(['phone'=>$data]);
                    break;
                case 'logistics':
                    $modelData=new Sn($modelData);
                    if($edition==4 || $edition==5){
                        $list=$modelData->Querys(['id'=>$data]);
                    }else{
                        $list=$modelData->Querys(['logistics'=>$data]);
                    }
                    break;
                default:
                    echo '参数错误';
            }
            $jsonlist=json_encode($list);
            if(!empty($jsonlist)){
                return $jsonlist;
            }else{
                return '{"警告":"数据不存在"}';
            }
        }
    }
 
    //此方法只返回数据库模型
    private function DbConfig($editionDb){
        $dbInfo=Dbinfo::Select($editionDb);
        $connection=new Connection([
            'dsn'=>"mysql:host=115.28.89.108;dbname={$dbInfo['name']}",
            'username'=>'heivr.store',
            'password'=>'rN86JZE7DDSS8nQP',
            'charset' => 'utf8',
        ]);
        return array('connection'=>$connection,'dbInfo'=>$dbInfo);//由于可能使用组合查询，这里将所有的数据库信息都返回
    }
}
//数据库文件保存
class Dbinfo{
    static protected $databaseArray=array(
        //韩国版
        'kr'=>array(
            'name'=>'edu_isk',//数据库名称
            'sn'=>'prod_edition_kr',//激活码
        ),
        //星球大冒险
        'star'=>array(
            'name'=>'edu_isk',//数据库名称
            'sn'=>'prod_edition',//激活码
            'user'=>'sys_user',//用户与产品对应表
            'code'=>'sys_user_captcha',//手机验证码
        ),
        //神卡王国1代
        'edu'=>array(
            'name'=>'heivr.store.edu',//数据库名称
            'sn'=>'heivr_edu_product',//激活码
        ),
        //主题乐园
        'press'=>array(
            'name'=>'heivr.store.press',//数据库名称
            'sn'=>'heivr_edu_product',//激活码
        ),
        //巨龙重生
        'dinosaur'=>array(
            'name'=>'heivr.store.dinosaur',//数据库名称
            'sn'=>'heivr_edu_product',//激活码
        ),
        //美术棒
        'art'=>array(
            'name'=>'edu_isk',//数据库名称
            'sn'=>'prod_art',//激活码
            'user'=>'sys_user',//用户与产品对应表
            'code'=>'sys_user_captcha',//手机验证码
        ),
    );
    static public function Select($name){
        return self::$databaseArray[$name];
    }
}